package com.financing.wap.service.impl;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.financing.wap.dao.IClientDao;
import com.financing.wap.dao.IClientRiskAnswerDao;
import com.financing.wap.dto.ClientRiskAnswerDTO;
import com.financing.wap.dto.ClientUserBaseDTO;
import com.financing.wap.dto.CorpClientUserBaseDTO;
import com.financing.wap.service.IClientService;
import com.financing.wap.util.RiskAssessQuestionUtil;
import com.financing.wap.vo.QueryClientUserVo;

@Service("clientService")
public class ClientServiceImpl implements IClientService{

	@Autowired
	private IClientDao clientDao;
	@Autowired
	private IClientRiskAnswerDao clientRiskQuestionDao;

	public List<ClientUserBaseDTO> getClientUserList(QueryClientUserVo queryVo) {

		return clientDao.getClientUserList(queryVo);
	}

	public int getClientUserListCount(QueryClientUserVo queryVo) {

		return clientDao.getClientUserListCount(queryVo);
	}

	public ClientUserBaseDTO searchClientUserBaseById(Integer id) {

		return clientDao.searchClientUserBaseById(id);
	}

	public int insertClientUserBase(ClientUserBaseDTO dto) {
		
		return clientDao.insertClientUserBase(dto);
	}

	public int updateClientUserBase(ClientUserBaseDTO dto) {
		return clientDao.updateClientUserBase(dto);
	}

	@Override
	public List<CorpClientUserBaseDTO> getCorpClientUserList(
			QueryClientUserVo queryVo) {
		
		return clientDao.getCorpClientUserList(queryVo);
	}

	@Override
	public int getCorpClientUserListCount(QueryClientUserVo queryVo) {
		
		return clientDao.getCorpClientUserListCount(queryVo);
	}

	@Override
	public int deleteClient(ClientUserBaseDTO dto) throws SQLException {
		return clientDao.deleteClient(dto);
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	@SuppressWarnings("unchecked")
	public int addClientAndQuestion(ClientUserBaseDTO dto, String qajson) throws Exception {
		int clientId = clientDao.insertClientUserBase(dto);
		
		JSONObject json = JSON.parseObject(qajson);
		Set<String> keySet = json.keySet();
		Iterator<String> keyIterator = keySet.iterator();
		
		//初始化 数据
		ClientRiskAnswerDTO riskdto = new ClientRiskAnswerDTO();
		riskdto.setUpdateBy(dto.getUpdateBy());
		riskdto.setCreateBy(dto.getCreateBy());
		riskdto.setCreateDate(dto.getCreateDate());
		riskdto.setUpdateDate(dto.getUpdateDate());
		riskdto.setuId(clientId+"");
		
		//计算风险评估分数
		List<Map<Object,Object>> questionPagerList =RiskAssessQuestionUtil.allQuestionsList;
		int score = 0;
		while(keyIterator.hasNext()){
			//获取题目、答案
			String key = keyIterator.next();
			String values = json.get(key).toString();
			riskdto.setqId(Integer.valueOf(key));
			riskdto.setaId(values);
			
			//获取答案分数
			Map<Object,Object> map  = questionPagerList.get(Integer.valueOf(key));
			List<Map<Object,Object>> answerList = (List<Map<Object,Object>>)map.get(RiskAssessQuestionUtil.ELEMENT_ANSWER_LIST);
			Map<Object,Object> answerListMap = answerList.get(Integer.valueOf(values)-1);
			
			String scoreStr = answerListMap.get(RiskAssessQuestionUtil.ELEMENT_SCORE).toString();
			score  = score+(Integer.valueOf(scoreStr).intValue());
			
			//添加客户风险评估答案
			clientRiskQuestionDao.insertClientRiskAnswer(riskdto);
		}
		dto.setcRiskScore(score);
		
		return clientDao.updateClientUserBase(dto);
	}

}
